package com.it.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;

/**
 * @Author: CaoYouGen
 * @DateTime: 2023/11/08/12:12
 * @注释: TODO
 **/
@Configuration
public class MySecurityUserConfig {

    @Bean
    public UserDetailsService userDetailsService(){
        // 创建两个用户
        UserDetails user1 = User.builder()
                .username("张三")
                .password(passwordEncoder().encode("123456"))
                .roles("student") // 角色的前面加上 ROLE_ 就成了权限
                .authorities("student_delete", "student_add") // 配置了权限
                .build();
        UserDetails user2 = User.builder()
                .username("李四")
                .password(passwordEncoder().encode("123456"))
                .authorities("teacher_delete", "teacher_add")
                .roles("teacher") // 配置了权限
                .build();
        // 创建一个内存用户详细信息管理器
        InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager();
        // 将两个用户放到 内存用户详细信息管理器中
        manager.createUser(user1);
        manager.createUser(user2);
        return manager;
    }

    /*
     * 自定义用户 必须配置密码编辑器
     * NoOpPasswordEncoder（没有加密）
     * @DateTime: 2023/11/7 21:11
     *
     * @return PasswordEncoder
     * @author: Coke
     */
    @Bean
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }
}
